<?php
$screen = Blyn::app()->getCurrentNode()->getUIManager()->getCurrentScreen();

//$url = $screen->getLinkUrl(array('bilyn_ajaxLink' => $ajaxLinkId));
$url = $screen->getLinkFromUrl(array('b_ajaxLinkId' => 'joinSpaceView' . "_" . "joinSpaceBtn"));
//$url = $screen->getAjaxLink($name);

$model = $screen->joinSpaceModel;

$spaces = $screen->findSpaces($model->searchText);
$spaceNameStr = Yii::t('site','Space Name:');
$joinToSpaceStr = Yii::t('site','Join to Space');
$joinedAlreadyStr = Yii::t('site','Joined already');
?>
<script type="text/javascript">
    /*<![CDATA[*/
    jQuery(function($) {
        //alert("hello");
        jQuery('a[rel="tooltip"]').tooltip();
        jQuery('a[rel="popover"]').popover();
        jQuery('a[rel="popover"]').click(function() {
            //$(this).css({'margin':'0 auto 0 auto','top':'20%','left':'20%'});    
            // $(this).center();
        });
    });

    jQuery.fn.center = function() {
        this.css("position", "absolute");
        this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) +
                $(window).scrollTop()) + "px");
        this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) +
                $(window).scrollLeft()) + "px");
        return this;
    }
    /*]]>*/
    function request_join_Space(spaceId)
    {
        //alert(spaceId);
        var message = (document.getElementById("joinSpace_message" + spaceId).value);
        var applyingRole = $("input:radio[name ='applyingRole']:checked").val();
        //document.getElementById("joinSpace_message"+spaceId).focus();
        //alert("#joinbtn" + spaceId);        

        $.ajax({
            type: 'POST',
            url: '<?php echo $url; ?>',
            data: {'joined_spaceId': spaceId, 'join_Space_message': message, 'applyingRole': applyingRole},
            success: function(html) {
                $('#show_userSpaceList_content').html(html);
                $("#joinbtn" + spaceId).addClass('disabled btn-warning').text(<?php echo $joinedAlreadyStr; ?>);
                $("#joinbtn" + spaceId).popover('hide');
            }

        });

    }

</script>

<?php
foreach ($spaces as $space) {
    $spaceName = $space->getName();
    //   $ajaxJoinBtn = $screen->buildAjaxLink('joinSpaceBtn', 'Join to Space', 'leftMenu', NULL, array('update' => "#leftbar", 'type' => 'post'), array('id' => 'joinBtn' . $space->getId(), 'class' => 'btn'));
    $spaceId = $space->getId();
    //$senderPermits = $screen->getSenderPermits($spaceId);
    //$allowRoles = current($senderPermits);
    ?>
    <div class="well well-small">
        <strong><?php echo $spaceNameStr; ?> <?php echo $spaceName; ?></strong><br>
        Normal Space<br>
        San Francisco, CA 94107     
        <div class="pull-right">
            <a href="#myModal<?php echo $spaceId; ?>" role="button" class="btn" data-toggle="modal"><?php echo $joinToSpaceStr; ?></a>
        </div>   
        <!-- Modal -->
        <div id="myModal<?php echo $spaceId; ?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <?php echo CHtml::beginForm(); ?>
            <fieldset>
                <?php $screen->renderView('applyJoinSpace', array('spaceId' => $spaceId)); ?>
            </fieldset>
            <?php echo CHtml::endForm(); ?>
        </div>
    </div>
<?php } ?>




